package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.core.user.SearchUserBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.bean.vo.core.user.ListUserVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.SelectVo;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.HeaderUtil;


/**
 * 查询用户 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("userRead")
public class UserRead {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	@Qualifier("userDeptRead")
	private UserDeptRead userDeptRead;
	
	@Autowired
	@Qualifier("deptRead")
	private DeptRead deptRead;
	
	@Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	/**
	 * 根据公司查询用户列表
	 * @param 
	 * @return
	 */
	public List<SelectVo> listSelectByCompany(Long companyId) {
		List<SelectVo> result = new ArrayList<SelectVo>();
		List<User> userList = userDao.listByCompany(Arrays.asList(companyId));
		for (User u : userList) {
			SelectVo sv = new SelectVo(u.getUserId().intValue(), u.getName(), "");
			result.add(sv);
		}
		return result;
	}
	
	/**
	 * 根据公司列表查询
	 * 用户Id列表
	 * @param area
	 * @return
	 */
	public Object listUserIdByCompany(List<Long> companyIdList) {
		List<User> userList = userDao.listByCompany(companyIdList);
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
	
	/**
	 * 根据公司查询
	 * 用户Id列表
	 * @param area
	 * @return
	 */
	public Object listUserIdByCompany(Long companyId) {
		List<User> userList = userDao.listByCompany(Arrays.asList(companyId));
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
    
	
	/**
	 * 访客
	 * 用户Id列表
	 * @return
	 */
	public Object listUserIdUnbind() {
		List<User> userList = userDao.listUnbind();
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
	
	/**
	 * 用户Id列表
	 * @param area
	 * @return
	 */
	public Object listUserId(Integer area) {
		List<User> userList = userDao.listAll(area);
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
	
	/**
	 * 访客
	 * 用户
	 * @return
	 */
	public Object listUserUnbind() {
		List<User> userList = userDao.listUnbind();
		List<ListVo> result = new ArrayList<>();
		transferList(userList, result);
		return result;
	}
	
	/**
	 * 用户
	 * @param area
	 * @return
	 */
	public Object listUser(Integer area) {
		List<User> userList = userDao.listAll(area);
		List<ListVo> result = new ArrayList<>();
		transferList(userList, result);
		return result;
	}
	
	private void transferList(List<User> userList, List<ListVo> result) {
		for (User item : userList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getUserId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 用户列表
	 * @param bo
	 * @return
	 * @throws WpsException 
	 */
	public Object list(SearchUserBo bo) throws WpsException {
		PageVo<ListUserVo> result = new PageVo<ListUserVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transfer(userDao.list(bo)));
        result.setTotal(userDao.countList(bo));
		return result;
	}
	
	/**
	 * 下拉框
	 * 用户列表
	 * @param bo
	 * @return
	 * @throws WpsException 
	 */
	public List<SelectVo> listUser(SearchUserBo bo) throws WpsException {
		List<SelectVo> result = userDao.listUser(bo);
		return result;
	}
	
	private List<ListUserVo> transfer(List<ListUserVo> list) throws WpsException {
		for (ListUserVo item : list) {
			//DetailCompanyVo company = (DetailCompanyVo) companyRead.detail(item.getCompanyId());
			//List<Long> deptIdList = (List<Long>) userDeptRead.listDeptId(item.getUserId());
			//List<DeptBean> deptList = new ArrayList<>();
			//if (!deptIdList.isEmpty()) {
			//	deptList = deptRead.listById(deptIdList);
			//}
			//item.setCompanyName(company.getName());
			//item.setDeptList(deptList);
			String pic = item.getPic();
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKey(pic);
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			String url = downloadVo.getUrl();
			item.setUrl(url);
		}
		return list;
	}

	/**
	 * 用户详情
	 * @param userId
	 * @return
	 * @throws WpsException
	 */
	public String picUrl(Long userId) throws WpsException {
		Object entity = userDao.detail(userId);
		if (entity == null) {
			throw new WpsException(ExceptionCode.数据不存在, "userId: " + String.valueOf(userId) 
														 + ", url: " + String.valueOf(HeaderUtil.getCurrentUrl())
														 + ", token: " + String.valueOf(HeaderUtil.getCurrentTokenFromTl())
														 + ", param: " + String.valueOf(HeaderUtil.getCurrentParam()));
		}
		String pic = ((DetailUserVo) entity).getPic();
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKey(pic);
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		String result = downloadVo.getUrl();
		return result;
	}
	

	/**
	 * 用户详情
	 * @param userId
	 * @return
	 * @throws WpsException
	 */
	@SuppressWarnings("unchecked")
	public Object detail(Long userId) throws WpsException {
		Object result = userDao.detail(userId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在, "userId: " + String.valueOf(userId) 
					 + ", url: " + String.valueOf(HeaderUtil.getCurrentUrl())
					 + ", token: " + String.valueOf(HeaderUtil.getCurrentTokenFromTl())
					 + ", param: " + String.valueOf(HeaderUtil.getCurrentParam()));
		}
		((DetailUserVo) result).setDeptIdList((List<Long>) userDeptRead.listDeptId(userId));
		return result;
	}
	
	
	public List<User> list(String username) {
		List<User> result = userDao.listByUsername(username);
		return result;
	}
	
	public List<User> listNotId(String username, Long userId) {
		List<User> result = new ArrayList<User>();
		List<User> userList = list(username);
		for (User c : userList) {
			if (!c.getUserId().equals(userId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	public List<User> login(String username, String pwd) {
		List<User> result = userDao.listByLogin(username, pwd, null);
		return result;
	}
	
	public List<User> listByOpenId(String openId) {
		List<User> result = userDao.listByOpenId(openId, null);
		return result;
	}
	
	

}
